using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.Application.Contracts.Customized.RoomSourceVerifyV1.Dtos;
using SOEI.Solution.DomainEntities.RoomSourceVerifiyV1.DomainService;
using SOEI.Solution.DomainEntities.RoomSourceVerifiyV1.Dtos;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace SOEI.Solution.DomainEntities.RoomSourceVerifiyV1
{
    /// <summary>
    /// 【扩展模块】  <br/>
    /// 【功能描述】  ：XXX 模块<br/>
    /// 【创建日期】  ：2020.05.21 <br/>
    /// 【开发人员】  ：static残影<br/>
    ///</summary>
    [ApiExplorerSettings(GroupName = "Manager", IgnoreApi = true)]
    public class CommissionContractDigestAppService : SolutionAppServiceBase, ICommissionContractDigestAppService
    {
        /// <summary>
        ///【CommissionContractDigest】仓储层
        /// </summary>
        private readonly IRepository<CommissionContractDigest, string> _commissioncontractdigestRepository;

        /// <summary>
        ///【CommissionContractDigest】领域服务
        /// </summary>
        private readonly ICommissionContractDigestManager _commissioncontractdigestManager;

        public CommissionContractDigestAppService(
            IRepository<CommissionContractDigest, string> commissioncontractdigestRepository,
            ICommissionContractDigestManager commissioncontractdigestManager
        )
        {
            _commissioncontractdigestRepository = commissioncontractdigestRepository;
            _commissioncontractdigestManager = commissioncontractdigestManager;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        ///【CommissionContractDigest】获取的分页列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<PagedResultDto<CommissionContractDigestListDto>> GetPaged(GetCommissionContractDigestsInput input)
        {
            var query = _commissioncontractdigestRepository.GetAll()
                          //模糊搜索 字段Id
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Id.Contains(input.FilterText))
                          //模糊搜索 字段STRJJJG
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.STRJJJG.Contains(input.FilterText))
                          //模糊搜索 字段JJJGBAH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.JJJGBAH.Contains(input.FilterText))
                          //模糊搜索 字段WTHTDJH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.WTHTDJH.Contains(input.FilterText))
                          //模糊搜索 字段CQZH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CQZH.Contains(input.FilterText))
                          //模糊搜索 字段ZCS
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ZCS.Contains(input.FilterText))
                          //模糊搜索 字段SZC
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.SZC.Contains(input.FilterText))
                          //模糊搜索 字段HX
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.HX.Contains(input.FilterText))
                          //模糊搜索 字段CX
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CX.Contains(input.FilterText))
                          //模糊搜索 字段ZXQK
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ZXQK.Contains(input.FilterText))
                          //模糊搜索 字段WTFS
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.WTFS.Contains(input.FilterText))
                          //模糊搜索 字段WTHTZTDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.WTHTZTDM.Contains(input.FilterText))
                          //模糊搜索 字段WTHTBLZTDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.WTHTBLZTDM.Contains(input.FilterText))
                          //模糊搜索 字段CQZBH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CQZBH.Contains(input.FilterText))
                          //模糊搜索 字段JJRYBH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.JJRYBH.Contains(input.FilterText))
                          //模糊搜索 字段JJRYXBRBH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.JJRYXBRBH.Contains(input.FilterText))
                          //模糊搜索 字段CQZDDCQ
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CQZDDCQ.Contains(input.FilterText))
                          //模糊搜索 字段YJBZDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.YJBZDM.Contains(input.FilterText))
                          //模糊搜索 字段SFZLDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.SFZLDM.Contains(input.FilterText))
                          //模糊搜索 字段ZLXX
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ZLXX.Contains(input.FilterText))
                          //模糊搜索 字段SFDY
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.SFDY.Contains(input.FilterText))
                          //模糊搜索 字段JJRYMC
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.JJRYMC.Contains(input.FilterText))
                          //模糊搜索 字段ISNEW
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ISNEW.Contains(input.FilterText))
                          //模糊搜索 字段BSM_BDCQZ
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.BSM_BDCQZ.Contains(input.FilterText))
                          //模糊搜索 字段WTRXM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.WTRXM.Contains(input.FilterText))
                          //模糊搜索 字段WTRZJHM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.WTRZJHM.Contains(input.FilterText))
            ;
            // TODO:根据传入的参数添加过滤条件

            var count = await query.CountAsync();

            var entityList = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();

            var entityListDtos = ObjectMapper.Map<List<CommissionContractDigestListDto>>(entityList);

            return new PagedResultDto<CommissionContractDigestListDto>(count, entityListDtos);
        }

        /// <summary>
        ///【CommissionContractDigest】通过指定id获取MemberListDto信息
        /// </summary>
        [AbpAuthorize()]
        public async Task<CommissionContractDigestListDto> GetById(EntityDto<string> input)
        {
            var entity = await _commissioncontractdigestRepository.GetAsync(input.Id);

            var dto = ObjectMapper.Map<CommissionContractDigestListDto>(entity);
            return dto;
        }

        /// <summary>
        ///【Company】 获取编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<GetCommissionContractDigestForEditOutput> GetForEdit(EntityDto<string> input)
        {
            var output = new GetCommissionContractDigestForEditOutput();
            CommissionContractDigestEditDto editDto;

            if (!string.IsNullOrEmpty(input.Id))
            {
                var entity = await _commissioncontractdigestRepository.GetAsync(input.Id);
                editDto = ObjectMapper.Map<CommissionContractDigestEditDto>(entity);
            }
            else
            {
                editDto = new CommissionContractDigestEditDto();
            }
            output.CommissionContractDigest = editDto;
            return output;
        }
        /// <summary>
        ///【CommissionContractDigest】 添加或者修改的公共方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task CreateOrUpdate(CreateOrUpdateCommissionContractDigestInput input)
        {
            if (!string.IsNullOrEmpty(input.CommissionContractDigest.Id))
            {
                await Update(input.CommissionContractDigest);
            }
            else
            {
                await Create(input.CommissionContractDigest);
            }
        }
        /// <summary>
        ///【CommissionContractDigest】新增
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task<CommissionContractDigestEditDto> Create(CommissionContractDigestEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = ObjectMapper.Map<CommissionContractDigest>(input);
            //调用领域服务
            entity = await _commissioncontractdigestManager.CreateAsync(entity);

            var dto = ObjectMapper.Map<CommissionContractDigestEditDto>(entity);
            return dto;
        }

        /// <summary>
        ///【CommissionContractDigest】编辑
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task Update(CommissionContractDigestEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新
            var key = input.Id;
            var entity = await _commissioncontractdigestRepository.GetAsync(key);
            //  input.MapTo(entity);
            //将input属性的值赋值到entity中
            ObjectMapper.Map(input, entity);
            await _commissioncontractdigestManager.UpdateAsync(entity);
        }

        /// <summary>
        ///【CommissionContractDigest】删除信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task Delete(EntityDto<string> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _commissioncontractdigestManager.DeleteAsync(input.Id);
        }

        /// <summary>
        ///【CommissionContractDigest】 批量删除Member的方法
        /// </summary>
        [AbpAuthorize()]
        public async Task BatchDelete(List<string> input)
        {
            // TODO:批量删除前的逻辑判断，是否允许删除
            await _commissioncontractdigestManager.BatchDelete(input);
        }

        #endregion

        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展应用服务实现*/

        /// <summary>
        ///【CommissionContractDigest】 委托协议【摘要】和【正文】保存
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<CommissionContractInput> CommissionContractDigestAndMainBodySave(CommissionContractInput input)
        {
            await Task.Run(() => { });

            return null;
        }

        #endregion
    }
}
